<template>
  <div>
    <h1>文章视频管理</h1>
    <el-button type="primary" icon="el-icon-edit" circle @click="openAdd" >添加文章或录播</el-button>
    <el-dialog
      title="添加文章或录播"
      :visible.sync="dialogVisible"
      width="70%">
      <el-form ref="form" :model="courseDTO.course" label-width="80px">
        <el-form-item label="课堂标题">
          <el-input v-model="courseDTO.course.courseTitle"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-radio-group v-model="courseDTO.course.courseType">
            <el-radio label="1">文章</el-radio>
            <el-radio label="2">录播</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="积分">
          <el-input v-model="courseDTO.course.integral"></el-input>
        </el-form-item>
        <el-form-item label="资源">
          <el-upload
            class="upload-demo"
            action="http://localhost:7000/course/uploadFile"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="3"
            :on-success="handleAvatarSuccess"
            :on-exceed="handleExceed"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="专家">
          <el-select v-model="courseDTO.course.specialistId" placeholder="请选择专家">
            <el-option
              v-for="item in expertList"
              :key="item.specialistId"
              :label="item.specialistName"
              :value="item.specialistId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addCourse">添加课堂</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

    <el-table
      :data="courseList"
      style="width: 100%">
      <el-table-column
        prop="courseId"
        label="id"
        width="180">
      </el-table-column>
      <el-table-column
        prop="courseTitle"
        label="课堂标题"
        width="180">
      </el-table-column>
      <el-table-column
        prop="courseType"
        label="课堂类型"
        width="180">
        <template slot-scope="scope" >
          <span v-if="scope.row.courseType==1" >文章</span>
          <span v-if="scope.row.courseType==2" >录播</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="adminId"
        label="上传运营人id"
        width="180">
      </el-table-column>
      <el-table-column
        prop="specialistId"
        label="专家id"
        width="180">
      </el-table-column>
      <el-table-column
        prop="integral"
        label="需要积分数"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="180">
        <template slot-scope="scope" >
          <el-button type="primary" @click="showResource(scope.row)">查看资源</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page.sync="pageNum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="searchCourse"
      @current-change="searchCourse">
    </el-pagination>


    <el-dialog
      title="资源"
      :visible.sync="dialogVisible2"
      width="30%">
      <span v-if="type==1" >
        <span v-for="item in resourceList" :key="item.resourceId"  ><a :href="item.resourceUrl" >详情</a><br/></span><br>
      </span>
      <span v-if="type==2" >
        <video   v-for="item in resourceList"  :key="item.resourceId"  width="200px"  :src="item.resourceUrl"  ></video>
      </span>

    </el-dialog>


  </div>
</template>
<script>
import {getSpecialistAll,addCourse,searchCourse} from '@/api/course'
export default {
  data(){
    return{
      dialogVisible: false,
      courseDTO:{
        course:{
          adminId:1
        },
        resource:[]
      },
      fileList:[],
      expertList:[],
      pageNum:1,
      pageSize:5,
      courseList:[],
      dialogVisible2:false,
      resourceList:[],
      total:0,
      type:1
    }
  },
  mounted() {
    getSpecialistAll(1,100).then(res=>{
      this.expertList=res.data
    })
    this.searchCourse()
  },
  methods:{
    showResource(row){
      this.dialogVisible2=true
      this.resourceList=row.resourceList
      if(row.courseType==1){
        this.type=1
      }else if(row.courseType==2){
        this.type=2
      }
    },
    searchCourse(){
      searchCourse(this.pageNum,this.pageSize).then(res=>{
        this.courseList=res.data
        this.total=res.total
      })
    },
    openAdd(){
      this.dialogVisible=true
    },
    addCourse(){
      addCourse(this.courseDTO).then(res=>{
        this.$message.success(res.msg)
        this.courseDTO={}
      })
    },
    handleAvatarSuccess(res, file) {
      this.courseDTO.resource.push(res)
    },
    handleRemove(file, fileList) {
      console.log(file);
      this.courseDTO.resource.pop()
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    }

  }
}
</script>
<style>

</style>
